const mongoose = require('mongoose');

const schema = new mongoose.Schema({
    {% for column in table['columns'] -%}
    {% if column['name'] != 'id' and column['ref'] == '' -%}
    {{column['name']}}: {
        type: {{type[column['type']]}},
        required: {{'true' if column['require'] else 'false'}}{{ ',' if column['default'] != '' else ''}}
        {%- if column['default'] != '' and column['type'] != 'datetime' %}
        default: {{column['default']}}
        {%- elif column['default'] != '' and column['type'] == 'datetime' %}
        default: Date.now
        {%- endif %}
    }{{ ',' if not loop.last else ''}}
    {% elif column['ref'] != '' -%}
    {{column['name']}}: {
        type: mongoose.Schema.Types.ObjectId,
        required: {{'true' if column['require'] else 'false'}},
        ref:'{{column['ref']}}'
    }{{ ',' if not loop.last else ''}}
    {% endif %}
    {%- endfor %}
});

schema.set('toJSON', { virtuals: true });
// schema.set('toObject', { virtuals: true });
module.exports = mongoose.model({{"'" + table['name'] + "'"}}, schema); //collection name